package com.linb.pan.server.common.stream.consumer.log;

import com.linb.pan.core.utils.IdUtil;
import com.linb.pan.server.common.stream.channel.PanChannels;
import com.linb.pan.server.common.stream.event.log.ErrorLogEvent;
import com.linb.pan.server.modules.log.entity.BPanErrorLog;
import com.linb.pan.server.modules.log.service.IBPanErrorLogService;
import com.linb.pan.stream.core.AbstractConsumer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.stream.annotation.StreamListener;
import org.springframework.messaging.Message;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

import java.util.Date;

@Component
public class ErrorLogEventListener extends AbstractConsumer {

    @Autowired
    private IBPanErrorLogService errorLogService;

    /**
     * 监听文件删除失败事件，保存到日志中
     */
    @StreamListener(PanChannels.ERROR_LOG_INPUT)
    @Async(value = "eventListenerTaskExecutor")
    public void saveErrorLog(Message<ErrorLogEvent> message) {
        if (isEmptyMessage(message)) {
            return;
        }
        printLog(message);
        ErrorLogEvent event = message.getPayload();

        BPanErrorLog bPanErrorLog = new BPanErrorLog();
        bPanErrorLog.setId(IdUtil.get());
        bPanErrorLog.setLogContent(event.getErrorMsg());
        bPanErrorLog.setLogStatus(0);
        bPanErrorLog.setCreateUser(event.getUserId());
        bPanErrorLog.setCreateTime(new Date());
        bPanErrorLog.setUpdateUser(event.getUserId());
        bPanErrorLog.setUpdateTime(new Date());
        errorLogService.save(bPanErrorLog);
    }

}
